import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/component'
import { connect } from '@tarojs/redux'

import './practice.less'

import Enum from '../../constants/Enum'
import Baige from '../../component/Baige'

const { categoryMap } = Enum

@connect(((state) => state))
class Practice extends Component {
  config = {
    navigationBarTitleText: '加法练习',
    navigationBarBackgroundColor: '#6C8AF8',
    navigationBarTextStyle: 'white'
  }

  getSource = () => {
    const { client } = this.props;
    const { type, range } = client.method;
    const category = categoryMap[type + range]
    /*
      减法 10 - 19 需要特殊处理
      y 10-19
      x 0-9
    */
   if (
     type === Enum.subtraction &&
     range === Enum.range_10_19
    ) {
      let y = []
      let x = []
      for (let i = 0; i < 20; i++) {
        if (i >= 10) {
          y.push(i);
        } else {
          x.push(i);
        }
      }
      return {y ,x, category}
   }

   let arr = [];
    for (let i = 0; i < 10; i++) {
      arr.push(i);
    }
    return {
      category,
      x: arr.concat().sort(() => Math.random() > 0.5 ? 1 : -1),
      y: arr.concat().sort(() => Math.random() > 0.5 ? -1 : 1)
    }
  }
  render() {
    return (
      <View>
        <Baige
          data={this.getSource()}
        />
      </View>
    )
  }
}

export default Practice;
